package com.core.pdf;

import javax.imageio.ImageIO;
import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterJob;
import java.io.*;

public class BarcodeMain {
    public static void main(String[] args) {
        try {

//            getBarcode("84040590000638723117");
            getBarcode("2904131XGW01A24012200001");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void getBarcode(String xxx) throws Exception {


        InputStream inputStream = null;
        ByteArrayOutputStream os = null;
        try {

            BufferedImage image = BarCodeUtils.getBarCodeWithWords(xxx, xxx);


            ImageIO.write(image, "jpg", new File("C:\\Users\\Administrator\\Desktop\\test\\" + System.currentTimeMillis() + ".jpg"));

            os = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", os);
            printImg(os.toByteArray());

            // 转入输出流
//            ImageIO.write(image, "png", os);

            // 直接导出到本地
            // ImageIO.write(image, "jpg", new File("D://AAAA.jpg"));
//            inputStream = new ByteArrayInputStream(os.toByteArray());

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }

            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static void printImg(byte[] dataFile) {
        try {
            DocFlavor dof =  DocFlavor.INPUT_STREAM.JPEG;
            //打印属性
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

            //文档属性
            DocAttributeSet das = new HashDocAttributeSet();
            // 设置打印纸张的大小（将像素转化为mm）
            das.add(new MediaPrintableArea(0, 0, 60,16, MediaPrintableArea.MM));
            Doc doc = new SimpleDoc(new ByteArrayInputStream(dataFile), dof, das);
            DocPrintJob printerJob = getPrintService().createPrintJob();
            printerJob.print(doc, pras);
        } catch (Exception ie) {
            ie.printStackTrace();
        }
    }


    /**
     * 取得打印机
     *
     * @return
     */
    private static PrintService getPrintService() {
        String printName = "Adobe PDF";
        PrintService printService = null;

        // 查找并设置打印机
        // 获得本台电脑连接的所有打印机
        PrintService[] printServices = PrinterJob.lookupPrintServices();
        if (printServices == null || printServices.length == 0) {
            return null;
        }

        //匹配指定打印机
        for (int i = 0; i < printServices.length; i++) {
            if (printServices[i].getName().equals(printName)) {
                printService = printServices[i];
                break;
            }
        }
        return printService;
    }

}
